﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AT_UHF_NET;
using System.Collections;

namespace AssetTrackDemo
{
    public partial class Reconciliate : Form, IUHFHost
    {
        AT_UHF_NET.CUHFHost rfid;
        int countMsg;
        AddAsset addAssetDialog = new AddAsset();
        List<Material> material = new List<Material>();
        List<Material> materialDB = new List<Material>();

        public Reconciliate()
        {
            InitializeComponent();

            rfid = new AT_UHF_NET.CUHFHost();
            rfid.ActivatedForm = this;
            rfid.PowerON();
            if (!rfid.Open())
                MessageBox.Show("Open fail");


            // for Read Accessed EPC value.
            rfid.SET_ReportMode(ReportMode.ACCESS_EPC, true);

            // for using session 0
            rfid.SET_Session(0);
            //rfid.SET_InventoryTarget(InventoryFlagMode.FLAG_AB);
        }

        public void GetReply(string Reply)
        {

        }

        public void GetAccessEPC(string str_epc)
        {

        }
        int ListCount;
        ArrayList alTID = new ArrayList();
        bool isReadingFlag = false;
        public void GetMemoryData(string Data)
        {
            if (isReadingFlag)
            {

                var e = materialDB.Find(x => x.TID == Data);
                if (e != null)
                {
                    lvItems.Items[Convert.ToInt32(e.ID)].BackColor= Color.Blue;
                }
                //while (isReadingFlag)
                {
                    //if (!alTID.Contains(Data))
                    //{
                    //    alTID.Add(Data);
                    //    AT_UHF_NET.CUHFHost.PlaySuccess();

                    //    ListCount = lvItems.Items.Count + 1;
                    //    ListViewItem lvItemMemData = new ListViewItem(ListCount.ToString());

                    //    lvItemMemData.SubItems.Add(Data);
                    //    var e = material.Find(x => x.TID == Data);
                    //    if (e != null)
                    //        lvItemMemData.SubItems.Add(e.Name);
                    //    else
                    //        lvItemMemData.SubItems.Add("");
                    //    this.lvItems.Items.Add(lvItemMemData);
                    //    this.lvItems.Refresh();
                    //}
                }
                return;
            }
            else
            {
                if (rfid.Open())
                    rfid.Stop();
            }
            return;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "Reconciliate")
            {
                btnStart.Text = "Stop";
                countMsg = 0;

                // you can use this function Inserting '0' in 5th parameter instead of "MEMBANK_CODE.XXX".
                // if you don't want read Accessed EPC value, just set report mode to that "SET_ReportMode(ReportMode.ACCESS_EPC, false)"
                // the way of using this functions "BankSelectRead_BankSelect" is same as BankSelectRead_BankSelect_Done()
                this.rfid.BankSelectRead_BankSelect(MEMBANK_CODE.BANK_TID,  // Memory Bank for reading
                                                    0,                      // start 
                                                    4,                      // length
                                                    true,                   // true = multi read, false = not multi
                                                    0,                      // Memory Bank for target tag (0 is not using parameter)
                                                    0,                      // Pattern's start address (bit) (0 is not using parameter)
                                                    null,                   // Pattern of memory bank for targeting (null is not using parameter)
                                                    null);                  // (can be omitted) – using password, if Membank is locked. (null is not using parameter)

                isReadingFlag = true;
            }
            else
            {
                btnStart.Text = "Reconciliate";
                isReadingFlag = false;
                rfid.Stop();
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            lvItems.Items.Clear();
            alTID.Clear();
            btnStart.Text = "Reconciliate";
            isReadingFlag = false;
            rfid.Stop();
        }

        private void btnAddAsset_Click(object sender, EventArgs e)
        {
            if (lvItems.SelectedIndices.Count > 0)
            {
                addAssetDialog.TID = lvItems.Items[lvItems.SelectedIndices[0]].SubItems[1].Text;
                addAssetDialog.ShowDialog();
            }
            else
            {
                MessageBox.Show("Please select an item to add");
            }
        }

        private void btnDBLookup_Click(object sender, EventArgs e)
        {
            try
            {
                PerformDBLookup();
                lvItems.Items.Clear();
                for (int i = 0; i < material.Count; i++)
                {
                    ListViewItem lvItemMemData = new ListViewItem((i + 1).ToString());
                    lvItemMemData.SubItems.Add(material[i].TID);
                    lvItemMemData.SubItems.Add(material[i].Name);
                    lvItemMemData.BackColor = Color.Red;
                    this.lvItems.Items.Add(lvItemMemData);
                    materialDB.Add(new Material(i, material[i].Name, material[i].TID));
                }
                this.lvItems.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error in DB Lookup");
            }
        }

        private void PerformDBLookup()
        {
            material = DataHelper.SelectAllMaterial();
            materialDB.Clear();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //PerformDBLookup();
            btnDBLookup_Click(sender, e);
        }
    }
}